package me.minecraft.plugin.itemrecycle.gui;

import lg.minecraft.plugin.library.inventory.CancelInventoryContext;
import lg.minecraft.plugin.library.inventory.ClickInventoryContext;
import lg.minecraft.plugin.library.inventory.InventoryContext;
import lg.minecraft.plugin.library.inventory.LGInventory;
import me.minecraft.plugin.itemrecycle.config.MainConfig;
import me.minecraft.plugin.itemrecycle.manager.DropItemManager;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;

/**
 * @author langle__
 * @version 1.0
 */
public class MainGUI extends LGInventory {

    private static final int[] GLASS_ARRAY = {46, 47, 48, 49, 50, 51, 52};
    private static final int PAGE_SIZE = 45;

    private int totalPages;
    private int page;

    public MainGUI(int page) {
        super(6, MainConfig.INSTANCE.getGuiTitle());
        this.page = page;
    }

    @Override
    public void onInventoryOpen(CancelInventoryContext context) {
        totalPages = (DropItemManager.INSTANCE.size() + PAGE_SIZE - 1) / PAGE_SIZE;

        int startIndex = page * PAGE_SIZE;
        int endIndex = Math.min(startIndex + PAGE_SIZE, DropItemManager.INSTANCE.size());

        inventory.clear();
        for (int i = 0; i < endIndex - startIndex; i++) {
            ItemStack dropItem = DropItemManager.INSTANCE.getDropItem(startIndex + i);
            inventory.setItem(i, dropItem);
        }

        for (int slot : GLASS_ARRAY) inventory.setItem(slot, MainConfig.INSTANCE.getBorderItem());

        inventory.setItem(45, MainConfig.INSTANCE.getPreviousPageItem(page + 1, totalPages));
        inventory.setItem(53, MainConfig.INSTANCE.getNextPageItem(page + 1, totalPages));
    }

    @Override
    public void onInventoryClick(ClickInventoryContext context) {
        context.setCancelled(true);
        Player player = context.getPlayer();
        int slot = context.getSlot();

        if (slot == 45 && page > 0) {
            page--;
            onInventoryOpen(context);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (slot == 53 && page < totalPages - 1) {
            page++;
            onInventoryOpen(context);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (slot >= 0 && slot < PAGE_SIZE) {
            int index = page * PAGE_SIZE + slot;
            ItemStack stack = DropItemManager.INSTANCE.removeDropItem(index);
            if (stack == null) return;
            HashMap<Integer, ItemStack> map = player.getInventory().addItem(stack);
            World world = player.getWorld();
            Location location = player.getLocation();
            for (ItemStack itemStack : map.values()) {
                world.dropItem(location, itemStack);
            }
            onInventoryOpen(context);
            player.playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, 0.5F, 1F);
        }
    }

    @Override
    public void onInventoryClose(InventoryContext context) {
    }

}
